package com.sgcc.pda.mdrh.task.safe.ui.ac;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.cepri.service.yingxiao.DtsManager;
import com.cepri.service.yingxiao.ITTSListener;
import com.cepri.service.yingxiao.baidutts.BaiduTtsManager;
import com.cepri.service.yingxiao.baidutts.ITtsManager;
import com.sgcc.pda.lib_tools_database.bean.other.TicketMeasureRisk;
import com.sgcc.pda.lib_tools_database.factory.KnowledgeCheck;
import com.sgcc.pda.mdrh.task.safe.R;
import com.sgcc.pda.mdrh.task.safe.entity.RiskBeforeResult;
import com.sgcc.pda.mdrh.task.safelibrary.BuildConfig;
import com.sgcc.pda.mdrh.utils.ToolGson2;
import com.sgcc.pda.mdrh.zoom.router.BaseOperateRouter;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述：防范措施展示页面
 * 作 者：小卷毛  2020-12-11 13:54
 * 修改描述：增加查询字段
 * 修 改 人：
 * 修改版本：v1.2.1
 */
public class SafeMeasureActivity extends FragmentActivity {

    public static final String MEASURE_APPNO = "safe_measure_appno";
    public static final String MEASURE_APPCODE = "safe_measure_appcode";
    public static final String MEASURE_ORDER = "safe_measure_eomordersrc";
    public static final String MEASURE_WORK = "safe_measure_worktypeno";
    public static final String MEASURE_PLANNO = "safe_measure_planno";
    public static final String MEASURE_FILTER = "safe_measure_filter";
    public static final String MEASURE_APPEND = "safe_measure_append";
    public static final String MEASURE_STEP = "safe_measure_steps";

    private ImageView ivVoice;
    private TextView tvContent;
    //初始化语音合成
    ITtsManager ttsManager;

    private static BaseOperateRouter.CompleteListener completeListener;

    public static void setCompleteListener(BaseOperateRouter.CompleteListener listener) {
        completeListener = listener;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.SafeTipDialogTheme);
        setContentView(R.layout.activity_safe_measure);
        initView();
        initData();
    }

    private void initView() {
        //初始化语音合成
        if (BuildConfig.USE_BAIDU_TTS)
            ttsManager = BaiduTtsManager.getInstance();
        else ttsManager = DtsManager.getInstance();
        ttsManager.init(this);

        ivVoice = findViewById(R.id.iv_voice);
        tvContent = findViewById(R.id.tv_content);

        // 点击确定打开页面或者关闭
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        // 点击确定打开页面或者关闭
        findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ivVoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (ttsManager.getPlayState()) {
                    case 0:
                        ttsManager.play("作业前风险点：" + tvContent.getText().toString());
                        break;
                    case 1:
                        ttsManager.pauseSpeaking();
                        break;
                    case 2:
                        ttsManager.resumeSpeaking();
                        break;
                }
            }
        });

        ttsManager.setPlayListener(new ITTSListener() {
            @Override
            public void onSpeakStart() {
                try {
                    Glide.with(SafeMeasureActivity.this)
                            .asGif()
                            .load(R.mipmap.icon_voice_gif)
                            .into(ivVoice);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSpeakPaused() {
                try {
                    Glide.with(SafeMeasureActivity.this)
                            .load(R.mipmap.icon_voice_b)
                            .into(ivVoice);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onSpeakResumed() {
                try {
                    Glide.with(SafeMeasureActivity.this)
                            .asGif()
                            .load(R.mipmap.icon_voice_gif)
                            .into(ivVoice);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onCompleted() {
                try {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Glide.with(SafeMeasureActivity.this)
                                    .load(R.mipmap.icon_voice)
                                    .into(ivVoice);
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initData() {
        try {
            Intent intent = getIntent();
            String appNo = intent.getStringExtra(MEASURE_APPNO);
            String appCode = intent.getStringExtra(MEASURE_APPCODE);
            String eomOrderSrc = intent.getStringExtra(MEASURE_ORDER);
            String workTypeNo = intent.getStringExtra(MEASURE_WORK);
            String planNo = intent.getStringExtra(MEASURE_PLANNO);
            String isFilter = intent.getStringExtra(MEASURE_FILTER);
            String isAppend = intent.getStringExtra(MEASURE_APPEND);
            ArrayList<String> steps = intent.getStringArrayListExtra(MEASURE_STEP);

            List<TicketMeasureRisk> list = new KnowledgeCheck()
                    .getTicketMeasureList(
                            appNo,
                            appCode,
                            eomOrderSrc,
                            workTypeNo,
                            planNo,
                            isFilter,
                            isAppend,
                            steps);

            StringBuilder content = new StringBuilder();
            for (int i = 0; i < list.size(); i++) {
                content.append(i + 1)
                        .append("、")
                        .append(list.get(i).getMeasure())
                        .append("\n");
            }

            tvContent.setText(content.toString());
            ttsManager.play("作业前风险点：" + content.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (ttsManager != null) {
            ttsManager.setPlayListener(null);
            ttsManager.stopSpeaking();
        }

        if (completeListener != null) {
            RiskBeforeResult result = new RiskBeforeResult("1", "成功");
            RiskBeforeResult.VerifyDetail verifyDetail =
                    new RiskBeforeResult.VerifyDetail(true, "已阅读作业前风险点！");
            result.setDTS(verifyDetail);

            completeListener.complete(1, ToolGson2.getJsonStr(result));
        }
    }

}
